package com.fltry.moreitem;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class MyAdapter extends RecyclerView.Adapter {
    private ArrayList<DataBean> data;
    private LayoutInflater inflater;

    public MyAdapter(ArrayList<DataBean> data, Context context) {
        this.data = data;
        inflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0)
            return new ViewHolder1(inflater.inflate(R.layout.item, parent, false));
        else if (viewType == 1)
            return new ViewHolder2(inflater.inflate(R.layout.item2, parent, false));
        else
            return new ViewHolder3(inflater.inflate(R.layout.item3, parent, false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DataBean dataBean = data.get(position);
        if (getItemViewType(position) == 0) {
            ((ViewHolder1) holder).iv.setImageResource(dataBean.getRes());
        } else if (getItemViewType(position) == 1) {
            ((ViewHolder2) holder).title.setText(dataBean.getTitle());
            ((ViewHolder2) holder).content.setText(dataBean.getContent());
        } else {
            ((ViewHolder3) holder).time.setText(dataBean.getTime());
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getType();
    }

    public class ViewHolder1 extends RecyclerView.ViewHolder {
        ImageView iv;

        public ViewHolder1(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.item_iv);
        }
    }

    public class ViewHolder2 extends RecyclerView.ViewHolder {
        TextView title;
        TextView content;

        public ViewHolder2(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.item_title);
            content = itemView.findViewById(R.id.item_content);
        }
    }

    public class ViewHolder3 extends RecyclerView.ViewHolder {
        TextView time;

        public ViewHolder3(View itemView) {
            super(itemView);
            time = itemView.findViewById(R.id.item_time);
        }
    }
}
